#define  _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"
#include "Queue.h"

void test()
{
	Stack s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);

	StackPush(&s, 4);
	StackPush(&s, 5);
	printf("%d ", StackSize(&s));
	while (!StackEmpty(&s))
	{
		printf("%d ", StackTop(&s));
		StackPop(&s);
	}

	StackDestroy(&s);
}

void test2()
{
	Queue Q;
	QueueInit(&Q);
	QueuePush(&Q, 1);
	QueuePush(&Q, 2);
	QueuePush(&Q, 3);
	QueuePush(&Q, 4);
	QueuePush(&Q, 5);

	while (!QueueEmpty(&Q))
	{
		printf("%d ", QueueFront(&Q));
		QueuePop(&Q);
	}
	//printf("%d ", QueueBack(&Q));
	//printf("%d ", QueueSize(&Q));
	//QueueEmpty(&Q);
	QueueDestroy(&Q);
}


typedef struct {
    Stack s1;
    Stack s2;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* q = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&q->s1);
    StackInit(&q->s2);
    return q;
}

void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->s1, x);
}

int myQueuePop(MyQueue* obj) {
    int x;
    while (obj->s1._top > 1)
    {
        x = StackTop(&obj->s1);
        StackPush(&obj->s2, x);
        StackPop(&obj->s1);
    }
    x = StackTop(&obj->s1);
    StackPop(&obj->s1);
    while (!StackEmpty(&obj->s2))
    {
        x = StackTop(&obj->s2);
        StackPush(&obj->s1, x);
        StackPop(&obj->s2);
    }
    return x;
}

int myQueuePeek(MyQueue* obj) {
    int x;
    while (obj->s1._top > 1)
    {
        x = StackTop(&obj->s1);
        StackPush(&obj->s2, x);
        StackPop(&obj->s1);
    }
    int ret = StackTop(&obj->s1);
    //StackPop(&obj->s1);
    while (!StackEmpty(&obj->s2))
    {
        x = StackTop(&obj->s2);
        StackPush(&obj->s1, x);
        StackPop(&obj->s2);
    }
    return ret;
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->s1);
}

void myQueueFree(MyQueue* obj) {
    StackDestroy(&obj->s1);
    StackDestroy(&obj->s2);
    free(obj);
    obj = NULL;
}

void test3()
{
   MyQueue* s = myQueueCreate();
    myQueuePush(s, 1);
    myQueuePush(s, 2);
    int ret = myQueuePeek(s);
    myQueuePop(s);
    myQueueFree(s);
}

int main()
{
	test3();
	return 0;
}